<?php

if (! function_exists('UTC2Normal')) {
    /**
     * UTC时间转正常时间
     *
     * @param $UTC
     * @param $format
     *
     * @return false|string
     */
    function UTC2Normal($UTC, $format)
    {
        return date($format, strtotime($UTC));
    }
}

if (! function_exists('bubbleSort')) {
    /**
     * @param $arr
     *
     * @return mixed
     */
    function bubbleSort($arr)
    {
        $len = count($arr);
        for ($i = 1; $i < $len; $i++) {
            for ($k = 0; $k < $len - $i; $k++) {
                if ($arr[$k] > $arr[$k + 1]) {
                    $tmp = $arr[$k + 1];
                    $arr[$k + 1] = $arr[$k];
                    $arr[$k] = $tmp;
                }
            }
        }

        return $arr;
    }
}

if (! function_exists('searchMax')) {
    /**
     * 取二维数组的某一项的最大值
     *
     * @param $arr
     * @param $field
     *
     * @return bool|mixed
     */
    function searchMax($arr, $field)
    {
        if (! is_array($arr) || ! $field) {
            return false;
        }

        $temp = [];
        foreach ($arr as $key => $val) {
            $temp[] = $val[$field];
        }

        return max($temp);
    }
}

if (! function_exists('searchMin')) {
    /**
     * 取二维数组的某一项的最小值
     *
     * @param $arr
     * @param $field
     *
     * @return bool|mixed
     */
    function searchMin($arr, $field)
    {
        if (! is_array($arr) || ! $field) {
            return false;
        }

        $temp = [];
        foreach ($arr as $key => $val) {
            $temp[] = $val[$field];
        }

        return min($temp);
    }
}

if (! function_exists('toDays')) {
    /**
     * 将日期转化为天数
     *
     * @param $date
     *
     * @return int
     */
    function toDays($date)
    {
        if (is_numeric($date)) {
            $res = 719528 + (int) ($date / 86400);
        } else {
            $TZ = date_default_timezone_get();
            date_default_timezone_set('UTC');
            $res = 719528 + (int) (strtotime($date) / 86400);
            date_default_timezone_set($TZ);
        }

        return $res;
    }
}

if (! function_exists('getFromDateRange')) {
    /**
     * 获取指定日期段内每一天的日期,可自定义格式输出
     *
     * @param  string  $start
     * @param  string  $end
     * @param  string  $format
     *
     * @return array
     */
    function getListFromDateRange($start, $end, $format = 'Y-m-d')
    {
        $startTimestamp = strtotime($start);
        $endTimestamp = strtotime($end);

        $days = ($endTimestamp - $startTimestamp) / 86400 + 1;

        $date = [];
        for ($i = 0; $i < $days; $i++) {
            $date[] = date($format, $startTimestamp + (86400 * $i));
        }

        return $date;
    }
}
if (! function_exists('array_rand_value')) {
    /**
     * Get a random value from an array.
     *
     * @param  array  $array
     * @param  int  $numReq  The amount of values to return
     *
     * @return mixed
     */
    function array_rand_value(array $array, $numReq = 1)
    {
        if (! count($array)) {
            return;
        }

        $keys = array_rand($array, $numReq);

        if ($numReq === 1) {
            return $array[$keys];
        }

        return array_intersect_key($array, array_flip($keys));
    }
}

if (! function_exists('array_rand_weighted')) {
    /**
     * Get a random value from an array, with the ability to skew the results.
     * Example: array_rand_weighted(['foo' => 1, 'bar' => 2]) has a 66% chance of returning bar.
     *
     * @param  array  $array
     *
     * @return mixed
     */
    function array_rand_weighted(array $array)
    {
        $array = array_filter($array, function ($item) {
            return $item >= 1;
        });

        if (! count($array)) {
            return;
        }
        $totalWeight = array_sum($array);

        foreach ($array as $value => $weight) {
            if (rand(1, $totalWeight) <= $weight) {
                return $value;
            }
            $totalWeight -= $weight;
        }
    }
}

if (! function_exists('values_in_array')) {
    /**
     * Determine if all given needles are present in the haystack.
     *
     * @param  array|string  $needles
     * @param  array  $haystack
     *
     * @return bool
     */
    function values_in_array($needles, array $haystack)
    {
        if (! is_array($needles)) {
            $needles = [$needles];
        }

        return count(array_intersect($needles, $haystack)) === count($needles);
    }
}

if (! function_exists('array_keys_exist')) {
    /**
     * Determine if all given needles are present in the haystack as array keys.
     *
     * @param  array|string  $needles
     * @param  array  $haystack
     *
     * @return bool
     */
    function array_keys_exist($needles, array $haystack)
    {
        if (! is_array($needles)) {
            return array_key_exists($needles, $haystack);
        }

        return values_in_array($needles, array_keys($haystack));
    }
}

if (! function_exists('array_split_filter')) {
    /**
     * Returns an array with two elements.
     * Iterates over each value in the array passing them to the callback function.
     * If the callback function returns true, the current value from array is returned in the first
     * element of result array. If not, it is return in the second element of result array.
     * Array keys are preserved.
     *
     * @param  array  $array
     * @param  callable  $callback
     *
     * @return array
     */
    function array_split_filter(array $array, callable $callback)
    {
        $passesFilter = array_filter($array, $callback);

        $negatedCallback = static function ($item) use ($callback) {
            return ! $callback($item);
        };

        $doesNotPassFilter = array_filter($array, $negatedCallback);

        return [$passesFilter, $doesNotPassFilter];
    }
}

if (! function_exists('array_split')) {
    /**
     * Split an array in the given amount of pieces.
     *
     * @param  array  $array
     * @param  int  $numberOfPieces
     * @param  bool  $preserveKeys
     *
     * @return array
     * @throws \InvalidArgumentException if the provided argument $numberOfPieces is lower than 1
     */
    function array_split(array $array, $numberOfPieces = 2, $preserveKeys = false)
    {
        if ($numberOfPieces <= 0) {
            throw new \InvalidArgumentException('Number of pieces parameter expected to be greater than 0');
        }

        if (count($array) === 0) {
            return [];
        }

        $splitSize = ceil(count($array) / $numberOfPieces);

        return array_chunk($array, $splitSize, $preserveKeys);
    }
}

if (! function_exists('array_merge_values')) {
    /**
     * Returns an array with the unique values from all the given arrays.
     *
     * @param  \array[]  $arrays
     *
     * @return array
     */
    function array_merge_values(array ...$arrays)
    {
        $allValues = array_reduce($arrays, static function ($carry, $array) {
            return array_merge($carry, $array);
        }, []);

        return array_values(array_unique($allValues));
    }
}

if (! function_exists('array_flatten')) {
    /**
     * Flatten an array of arrays. The `$levels` parameter specifies how deep you want to
     * recurse in the array. If `$levels` is -1, the function will recurse infinitely.
     *
     * @param  array  $array
     * @param  int  $levels
     *
     * @return array
     */
    function array_flatten(array $array, $levels = -1)
    {
        if ($levels === 0) {
            return $array;
        }

        $flattened = [];

        if ($levels !== -1) {
            --$levels;
        }

        foreach ($array as $element) {
            $flattened[] = is_array($element) ? array_flatten($element, $levels) : [$element];
        }

        return array_merge([], ...$flattened);
    }
}
//集合多个值排序
if (! function_exists('sortArrByManyField')) {
    function sortArrByManyField()
    {
        $args = func_get_args(); // 获取函数的参数的数组 
        if(empty($args)){ 
        return null; 
        } 
        $arr = array_shift($args); 
        if(!is_array($arr)){ 
        //   throw new Exception("第一个参数不为数组"); 
        } 
        foreach($args as $key => $field){ 
        if(is_string($field)){
            $temp = array();
            foreach($arr as $index=> $val){
            $temp[$index] = $val[$field];
            }
            $args[$key] = $temp;
        }
        }
        $args[] = &$arr;//引用值
        call_user_func_array('array_multisort',$args);
        return array_pop($args);
    }
}
